package com.zc.im.controller;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.zc.im.model.im.group.ZcImGroupComment;
import com.zc.im.service.im.group.ImGroupCommentService;
import com.zc.im.util.JsonUtils;
import com.zc.im.util.ResponseJson;
import com.zc.im.util.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 评论controller
 * Created by wangcc on 2017/7/8.
 */
@Controller
@RequestMapping(value = "/groupcomment")
public class ImGroupCommentController {

    @Autowired
    private ImGroupCommentService imGroupCommentService;
    private Logger logger = Logger.getLogger(ImGroupCommentController.class);

    /**
     * 评论内容插入
     * 添加评论json{"groupId":"2","logId":"2","content":"写的不好重写","commentUserId":"2","companyId":"2","commentUserId":"1274564"}
     * @param groupcomment
     * @return
     */
    @RequestMapping(value = "/insertgroupcoment",method = RequestMethod.POST)

    public ResponseJson insertGroupComment(String groupcomment){
        ResponseJson responseJson = new ResponseJson();
        if (StringUtils.isEmpty(groupcomment)) {
            return responseJson;
        }
        Gson gson = new Gson();
        ZcImGroupComment zcImGroupComment = gson.fromJson(groupcomment, ZcImGroupComment.class);
        Integer key = null;
        try {
          key = imGroupCommentService.saveGroupComment(zcImGroupComment);
        } catch (Exception e) {
          logger.error(e.getMessage(),e);
        }
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("key", key);
        responseJson.setDataMap(dataMap);
        return responseJson;
    }

    /**
     * 根据日志id查询评论
     * 传入的json{"logId":"要查询日志的id"}
     * @param logId
     * @return
     */
    @RequestMapping(value = "/queryGroupComment",method = RequestMethod.POST)
    public ResponseJson queryGroupComment(String logId) {
        ResponseJson responseJson = new ResponseJson();
        if(logId==null) {
            return  responseJson;
        }
        JSONObject jsonType = JsonUtils.strToJsonObj(logId);
        List<ZcImGroupComment> listZcImGroupComment = imGroupCommentService
        .listGroupComment(Integer.parseInt(jsonType.getString("logId")));
        Map<String ,Object> dataMap = new HashMap<>();
        dataMap.put("key", JsonUtils.objToStr(listZcImGroupComment));
        responseJson.setDataMap(dataMap);
        return responseJson;
    }
}
